﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using AMPLibrary.Media;
using AMPLibrary.Collections;

namespace AMPLibrary.Controls
{
    /// <summary>
    /// Interaction logic for AutomaticPlaylistDetailsControl.xaml
    /// </summary>
    public partial class AutomaticPlaylistDetailsControl : UserControl
    {
        private MusicCollection musicCollection = MusicCollection.GetMusicCollection();

        private AutomaticPlaylist currentAutomaticPlaylist;

        private bool isBinding = false;
        
        /// <summary>
        /// 
        /// </summary>
        public AutomaticPlaylistDetailsControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindBasics()
        {
            // bind genres
            try
            {
                this.genreListBox.ItemsSource = (from g in musicCollection orderby g.Tag.Genre select g.Tag.Genre).Distinct();
                this.genreListBox.Items.Refresh();
                this.genreListBox.Items.Insert(0, "No consideration");
            }
            catch
            {
            }

            // bind years
            try
            {
                this.yearListBox.ItemsSource = (from y in musicCollection orderby y.Tag.Year descending select y.Tag.Year).Distinct();
                this.yearListBox.Items.Refresh();
                this.yearListBox.Items.Insert(0, "No consideration");
            }
            catch
            {
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void BindAutomaticPlayistInformation()
        {
            isBinding = true;

            this.automaticPlaylistNameLabel.Content = currentAutomaticPlaylist.PlaylistName;

            this.itemLimitTextBox.Text = currentAutomaticPlaylist.ItemLimit.ToString();

            foreach (string genre in this.currentAutomaticPlaylist.Genres)
                this.genreListBox.SelectedItems.Add(genre);

            foreach (string year in this.currentAutomaticPlaylist.Years)
                this.yearListBox.SelectedItems.Add(year);

            switch (currentAutomaticPlaylist.MediaPlaybackFrequencyState)
            {
                case MediaPlaybackFrequencyState.Never:
                    frequencyConsiderationComboBox.SelectedIndex = 1;
                    break;
                case MediaPlaybackFrequencyState.NoConsideration:
                    frequencyConsiderationComboBox.SelectedIndex = 0;
                    break;
                case MediaPlaybackFrequencyState.RecentLast250:
                    frequencyConsiderationComboBox.SelectedIndex = 2;
                    break;
                case MediaPlaybackFrequencyState.RecentLast500:
                    frequencyConsiderationComboBox.SelectedIndex = 3;
                    break;
                case MediaPlaybackFrequencyState.RecentLast750:
                    frequencyConsiderationComboBox.SelectedIndex = 4;
                    break;
            }

            switch (currentAutomaticPlaylist.MediaFavoriteState)
            {
                case MediaFavoriteState.OnlyUseFavorites:
                    favoritesConsiderationComboBox.SelectedIndex = 1;
                    break;
                case MediaFavoriteState.UseAllMedia:
                    favoritesConsiderationComboBox.SelectedIndex = 0;
                    break;
                case MediaFavoriteState.UseAllMediaIgnoreFavorites:
                    favoritesConsiderationComboBox.SelectedIndex = 2;
                    break;
            }

            switch (currentAutomaticPlaylist.UserMediaRating)
            {
                case UserMediaRating.NoConsideration:
                    this.userMediaRating.SelectedIndex = 0;
                    break;
                case UserMediaRating.Unrated:
                    this.userMediaRating.SelectedIndex = 1;
                    break;
                case UserMediaRating.OneStar:
                    this.userMediaRating.SelectedIndex = 2;
                    break;
                case UserMediaRating.TwoStars:
                    this.userMediaRating.SelectedIndex = 3;
                    break;
                case UserMediaRating.ThreeStars:
                    this.userMediaRating.SelectedIndex = 4;
                    break;
                case UserMediaRating.FourStars:
                    this.userMediaRating.SelectedIndex = 5;
                    break;
                case UserMediaRating.FiveStars:
                    this.userMediaRating.SelectedIndex = 6;
                    break;
                case UserMediaRating.AtLeastOneStar:
                    this.userMediaRating.SelectedIndex = 7;
                    break;
                case UserMediaRating.AtLeastTwoStars:
                    this.userMediaRating.SelectedIndex = 8;
                    break;
                case UserMediaRating.AtLeastThreeStars:
                    this.userMediaRating.SelectedIndex = 9;
                    break;
                case UserMediaRating.AtLeastFourStars:
                    this.userMediaRating.SelectedIndex = 10;
                    break;
                case UserMediaRating.AtLeastFiveStars:
                    this.userMediaRating.SelectedIndex = 11;
                    break;
            }

            switch (currentAutomaticPlaylist.RecentlyAddedMedia)
            {
                case RecentlyAddedMedia.NoConsideration:
                    this.recentlyAddedConsiderationComboBox.SelectedIndex = 0;
                    break;

                case RecentlyAddedMedia.InTheLastWeek:
                    this.recentlyAddedConsiderationComboBox.SelectedIndex = 1;
                    break;

                case RecentlyAddedMedia.InTheLastTwoWeeks:
                    this.recentlyAddedConsiderationComboBox.SelectedIndex = 2;
                    break;

                case RecentlyAddedMedia.InTheLastMonth:
                    this.recentlyAddedConsiderationComboBox.SelectedIndex = 3;
                    break;

                case RecentlyAddedMedia.InTheLastThreeMonths:
                    this.recentlyAddedConsiderationComboBox.SelectedIndex = 4;
                    break;

                case RecentlyAddedMedia.InTheLastSixMonths:
                    this.recentlyAddedConsiderationComboBox.SelectedIndex = 5;
                    break;

                case RecentlyAddedMedia.InTheLastYear:
                    this.recentlyAddedConsiderationComboBox.SelectedIndex = 6;
                    break;
            }

            isBinding = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="automaticPlaylist"></param>
        public void BindAutomaticPlaylist(AutomaticPlaylist automaticPlaylist)
        {
            this.currentAutomaticPlaylist = automaticPlaylist;

            BindBasics();
            BindAutomaticPlayistInformation();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void itemLimitTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !AreAllValidNumericChars(e.Text);
            
            base.OnPreviewTextInput(e); 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool AreAllValidNumericChars(string str)
        {
            bool ret = true;

            for (int i = 0; i < str.Length; i++)
            {
                char ch = str[i];
                ret &= Char.IsDigit(ch);
            }

            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void genreListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.currentAutomaticPlaylist == null)
                return;

            if (isBinding == true)
                return;

            this.currentAutomaticPlaylist.Genres.Clear();

            foreach(string item in genreListBox.SelectedItems)
                this.currentAutomaticPlaylist.Genres.Add(item);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void yearListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.currentAutomaticPlaylist == null)
                return;

            if (isBinding == true)
                return;

            this.currentAutomaticPlaylist.Years.Clear();

            foreach (string item in yearListBox.SelectedItems)
                this.currentAutomaticPlaylist.Years.Add(item);
        }

        private void frequencyConsiderationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.currentAutomaticPlaylist == null)
                return;

            if (isBinding)
                return;

            int index = frequencyConsiderationComboBox.Items.IndexOf(e.AddedItems[0]);

            switch (index)
            {
                case 0:
                    this.currentAutomaticPlaylist.MediaPlaybackFrequencyState = MediaPlaybackFrequencyState.NoConsideration;
                    break;

                case 1:
                    this.currentAutomaticPlaylist.MediaPlaybackFrequencyState = MediaPlaybackFrequencyState.Never;
                    break;

                case 2:
                    this.currentAutomaticPlaylist.MediaPlaybackFrequencyState = MediaPlaybackFrequencyState.RecentLast250;
                    break;

                case 3:
                    this.currentAutomaticPlaylist.MediaPlaybackFrequencyState = MediaPlaybackFrequencyState.RecentLast500;
                    break;

                case 4:
                    this.currentAutomaticPlaylist.MediaPlaybackFrequencyState = MediaPlaybackFrequencyState.RecentLast750;
                    break;
            }
        }

        private void favoritesConsiderationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.currentAutomaticPlaylist == null)
                return;

            if (isBinding)
                return;

            int index = favoritesConsiderationComboBox.Items.IndexOf(e.AddedItems[0]);

            switch (index)
            {
                case 0:
                    this.currentAutomaticPlaylist.MediaFavoriteState = MediaFavoriteState.UseAllMedia;
                    break;

                case 1:
                    this.currentAutomaticPlaylist.MediaFavoriteState = MediaFavoriteState.OnlyUseFavorites;
                    break;

                case 2:
                    this.currentAutomaticPlaylist.MediaFavoriteState = MediaFavoriteState.UseAllMediaIgnoreFavorites;
                    break;
            }
        }

        private void userMediaRating_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.currentAutomaticPlaylist == null)
                return;

            if (isBinding)
                return;

            int index = userMediaRating.Items.IndexOf(e.AddedItems[0]);

            switch (index)
            {
                case 0:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.NoConsideration;
                    break;

                case 1:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.Unrated;
                    break;

                case 2:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.OneStar;
                    break;

                case 3:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.TwoStars;
                    break;

                case 4:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.ThreeStars;
                    break;

                case 5:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.FourStars;
                    break;

                case 6:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.FiveStars;
                    break;

                case 7:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.AtLeastOneStar;
                    break;

                case 8:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.AtLeastTwoStars;
                    break;

                case 9:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.AtLeastThreeStars;
                    break;

                case 10:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.AtLeastFourStars;
                    break;

                case 11:
                    this.currentAutomaticPlaylist.UserMediaRating = UserMediaRating.AtLeastFiveStars;
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void recentlyAddedConsiderationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.currentAutomaticPlaylist == null)
                return;

            if (isBinding)
                return;

            int index = recentlyAddedConsiderationComboBox.Items.IndexOf(e.AddedItems[0]);

            switch (index)
            {
                case 0:
                    this.currentAutomaticPlaylist.RecentlyAddedMedia = RecentlyAddedMedia.NoConsideration;
                    break;

                case 1:
                    this.currentAutomaticPlaylist.RecentlyAddedMedia = RecentlyAddedMedia.InTheLastWeek;
                    break;

                case 2:
                    this.currentAutomaticPlaylist.RecentlyAddedMedia = RecentlyAddedMedia.InTheLastTwoWeeks;
                    break;

                case 3:
                    this.currentAutomaticPlaylist.RecentlyAddedMedia = RecentlyAddedMedia.InTheLastMonth;
                    break;

                case 4:
                    this.currentAutomaticPlaylist.RecentlyAddedMedia = RecentlyAddedMedia.InTheLastThreeMonths;
                    break;

                case 5:
                    this.currentAutomaticPlaylist.RecentlyAddedMedia = RecentlyAddedMedia.InTheLastSixMonths;
                    break;

                case 6:
                    this.currentAutomaticPlaylist.RecentlyAddedMedia = RecentlyAddedMedia.InTheLastYear;
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void genereatePlaylistButton_Click(object sender, RoutedEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Wait;

            if (this.currentAutomaticPlaylist == null)
                return;

            this.currentSelectedPlaylistListBox.ItemsSource = this.currentAutomaticPlaylist.GenerateList(Convert.ToInt32(this.itemLimitTextBox.Text));
            this.currentSelectedPlaylistListBox.Items.Refresh();

            Mouse.OverrideCursor = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listenNowButton_Click(object sender, RoutedEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Wait;

            if (this.currentAutomaticPlaylist == null)
                return;

            if (this.currentSelectedPlaylistListBox.HasItems == false)
                return;

            NowPlayingCollection npc = NowPlayingCollection.GetNowPlayingCollection();

            // clear the now playing list
            npc.ClearItems();

            // for each selected media item
            foreach (MediaFile selectedMedia in this.currentSelectedPlaylistListBox.ItemsSource)
                npc.AddItem(selectedMedia);

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Next();

            Mouse.OverrideCursor = null;
        }
    }
}
